package com.teamcalendar.client.controller;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.teamcalendar.api.CreateJson;
import com.teamcalendar.api.ParseJson;
import com.teamcalendar.client.DBAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Base64;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MemberInformationActivity extends Activity {

	DBAdapter mDb=new DBAdapter(this);
	Bundle bundle;
	Long memberRowId;
	String path;
	Cursor mCursor;
	TextView firstNameView;
	TextView lastNameView;
	TextView emailView;
	TextView roleView;
	TextView phoneNumberView;
	String imageUrl;
	String imageName;
	ImageView imagView;
	String memberId;
	String senderId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_information);
		firstNameView=(TextView) findViewById(R.id.member_firstname);
		lastNameView=(TextView) findViewById(R.id.member_lastname);
		emailView=(TextView) findViewById(R.id.member_email);
		phoneNumberView=(TextView) findViewById(R.id.member_phonenumber);
		roleView=(TextView) findViewById(R.id.member_role);
		imagView=(ImageView) findViewById(R.id.member_imageView);
		mDb.open();
		bundle=getIntent().getExtras();
		memberRowId=bundle.getLong("memberRowId");
		path=bundle.getString("path");
		System.out.println(memberRowId);
		if (path.equals("myTeam")){
			mCursor=mDb.getMyTeamMember(memberRowId);
			memberId = bundle.getString("memberId");
			senderId = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_MEMBERID));
		}else{
			mCursor=mDb.getCurrentTeamMember(memberRowId);
		}
		mCursor.moveToFirst();
		firstNameView.setText(mCursor.getString(
				mCursor.getColumnIndex(DBAdapter.KEY_FIRSTNAME)));
		lastNameView.setText(mCursor.getString(
				mCursor.getColumnIndex(DBAdapter.KEY_LASTNAME)));
		emailView.setText(mCursor.getString(
				mCursor.getColumnIndex(DBAdapter.KEY_EMAIL)));
		SpannableString content = new SpannableString(mCursor.getString(
				mCursor.getColumnIndex(DBAdapter.KEY_PHONENUMBER)));
		content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
		phoneNumberView.setText(content);
		roleView.setText(mCursor.getString(
				mCursor.getColumnIndex(DBAdapter.KEY_ROLE)));

		imageName=mCursor.getString(
				mCursor.getColumnIndex(DBAdapter.KEY_MEMBER_IMAGE));
		mCursor.close();
		byte[] image = null;


		if(!imageName.equals("")){
			
			mCursor=mDb.getImage(imageName);
			mCursor.moveToFirst();
			String temp=mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_IMAGE));
			image=Base64.decode(temp, Base64.DEFAULT);
			Bitmap bitMap=BitmapFactory.decodeByteArray(
					image, 0, image.length);
			Display display = getWindowManager().getDefaultDisplay();
			int maxWidth=display.getWidth();
			int maxHeight=display.getHeight();
			if(maxWidth>maxHeight){
				maxWidth=maxWidth*1/6;
				maxHeight=maxHeight*1/2;
			}else{
				maxWidth=maxWidth*1/2;
				maxHeight=maxHeight*1/2;
			}
			imagView.setImageBitmap(resizeImage(bitMap, maxWidth, maxHeight));
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
	};

	

	private void writeMessage() {
		Intent intent = new Intent(MemberInformationActivity.this, WriteMessageActivity.class);
		intent.putExtra("memberId", memberId);
		intent.putExtra("senderid", senderId);
		intent.putExtra("message", "");
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.message_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.menu_message_writemessage:
			writeMessage();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		
		if (path.equals("myTeam")) {
			menu.add(0, R.id.menu_message_writemessage, 0, "Skriv meddelande");			
		}
		return super.onPrepareOptionsMenu(menu);
	}
	
	public void call(View view) {
		// prepare the alert box
		AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
		alertbox.setTitle("Medelande!");
		// set the message to display
		alertbox.setMessage("Vill du verkligen ringa?");

		// set a positive/yes button and create a listener
		alertbox.setPositiveButton("Ja", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				String number = phoneNumberView.getText().toString();
				Intent phone = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
				startActivity(phone);
				
			}
			
		});
		
		alertbox.setNegativeButton("Nej", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		alertbox.show();
	}
	
	private Bitmap resizeImage( final Bitmap image, final int maxWidth, final int maxHeight ) {
		final Bitmap resizedImage;
		int imageHeight = image.getHeight();
		if ( imageHeight > maxHeight )
		imageHeight = maxHeight;
		int imageWidth = (imageHeight*image.getWidth()) / image.getHeight();
		if ( imageWidth > maxWidth ) {
		imageWidth = maxWidth;
		imageHeight = (imageWidth*image.getHeight()) / image.getWidth();
		}
		resizedImage = Bitmap.createScaledBitmap( image, imageWidth, imageHeight, true);
		return resizedImage;
		} 

}
